<template>
	<u-gap height="20"></u-gap>
	<view class="u-p-l-28 u-p-r-28 u-rela">
		<view class="bgw box-radius u-p-24 u-m-b-20" v-for="(v,i) in historyAccount">
			<view class="u-flex">
				<u-avatar :src="baseImgUrl+v.avatar" size="92" v-if="v.avatar"></u-avatar>
				<u-avatar :src="defaultAvatar" size="120" v-else></u-avatar>
				<view class="u-flex-1 u-m-l-20">
					<view class="">
						{{hidePhone(v.userName)}}
					</view>
					<view class="u-p-t-10">
						<text class="tag-min-green" v-if="userInfo && userInfo.userName == v.userName">当前账号</text>
						<text class="tag-min-grey" v-else>历史账号</text>
					</view>
				</view>
				<u-button size="mini" :plain="true" @click="logout" type="warning" shape="circle"v-if="userInfo && userInfo.userName == v.userName">退出</u-button>
				<u-button size="mini" @click="switchAccount(v)" type="warning" shape="circle" v-else>切换</u-button>
			</view>
		</view>
		<view class="bgw box-radius u-p-24 u-m-b-20"  @click="nav('/pages/login/login?isBack=1')">
			<view class="u-flex">
				<image src="/static/images/icon-add.png" mode="widthFix" style="width: 92upx;"></image>
				<view class="u-flex-1 u-m-l-20">
					添加新账号
				</view>
			</view>
		</view>
	</view>
	<u-gap height="20"></u-gap>
</template>

<script setup>
	import store from '@/store/index.js'; //需要引入store
	import mixins from '@/common/js/mixins.js';
	import {
		ref,
		reactive,
		computed,
		getCurrentInstance
	} from 'vue'
	import {
		onReady,
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	const {
		proxy,
	} = getCurrentInstance()
	const {
		baseImgUrl
	} = proxy
	const {
		nav,
		navLogin,
		modal,
		hidePhone
	} = proxy.$util;
	const userInfo = computed(() => store.state.$userInfo)
	const isLogin = computed(() => {
		return store.getters.get_isLogin
	})
	const historyAccount = computed(() => {
		return store.getters.get_historyAccount
	})
	const logout = () =>{
		modal('确定退出当前账号？').then(() => {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			proxy.$u.api.logout().then(res => {
				let {
					code,
					msg,
					data
				} = res;
				if (code == 200) {
					store.dispatch('logout')
					uni.$u.toast(msg || '成功');
				} else {
					uni.$u.toast(msg || '错误');
				}
			})
		}).catch(() => {
		
		})
		
	}
	const switchAccount = (d) =>{
		let url = `/pages/login/login?isBack=1&userName=${d.userName}`;
		if(d.password){
			url += `&password=${d.password}`
		}
		nav(url)
	}
	onReady(() => {

	})
	onLoad((d) => {

	})
	onShow(() => {		
		// if(store.getters.get_isLogin){
		// 	store.dispatch('getUserInfo')
		// }
	})
</script>
